﻿Public Class clsInhZlecenie
    Inherits clsZlecenie

#Region "Attributes"

    Private mvarPozycjeZlecenia As Collection(Of clsInhPozycjaZlecenia)
    Private mvarPozycjeZleceniaDel As Collection(Of clsInhPozycjaZlecenia)

    Private mvarKlient As clsKlient
    Private mvarSposobZamowienia As clsSposobZamowienia
    Private mvarAdresWysylki As clsAdresWysylki
    Private mvarPracownik As clsPracownik
    Private mvarStatusZlecenia As clsStatus
    Private mvarStatusWysylki As clsStatusWysylki
    Private mvarOferta As clsInhOferta

    Private mvarWysylki As Collection(Of clsInhWysylka)
    Private mvarWysylkiDelete As Collection(Of clsInhWysylka)

#End Region

#Region "Properties"

    Public Property PozycjeZlecenia() As Collection(Of clsInhPozycjaZlecenia)
        Get
            Return mvarPozycjeZlecenia
        End Get
        Set(ByVal value As Collection(Of clsInhPozycjaZlecenia))
            mvarPozycjeZlecenia = value
        End Set
    End Property

    Public Property PozycjeZleceniaDel() As Collection(Of clsInhPozycjaZlecenia)
        Get
            Return mvarPozycjeZleceniaDel
        End Get
        Set(ByVal value As Collection(Of clsInhPozycjaZlecenia))
            mvarPozycjeZleceniaDel = value
        End Set
    End Property

    Public Property Klient As clsKlient
        Get
            Return mvarKlient
        End Get
        Set(ByVal value As clsKlient)
            mvarKlient = value
        End Set
    End Property

    Public Property SposobZamowienia As clsSposobZamowienia
        Get
            Return mvarSposobZamowienia
        End Get
        Set(ByVal value As clsSposobZamowienia)
            mvarSposobZamowienia = value
        End Set
    End Property

    Public Property AdresWysylki As clsAdresWysylki
        Get
            Return mvarAdresWysylki
        End Get
        Set(ByVal value As clsAdresWysylki)
            mvarAdresWysylki = value
        End Set
    End Property

    Public Property Pracownik As clsPracownik
        Get
            Return mvarPracownik
        End Get
        Set(ByVal value As clsPracownik)
            mvarPracownik = value
        End Set
    End Property

    Public Property StatusZlecenia As clsStatus
        Get
            Return mvarStatusZlecenia
        End Get
        Set(ByVal value As clsStatus)
            mvarStatusZlecenia = value
        End Set
    End Property

    Public Property Wysylka() As Collection(Of clsInhWysylka)
        Get
            Return mvarWysylki
        End Get
        Set(ByVal value As Collection(Of clsInhWysylka))
            mvarWysylki = value
        End Set
    End Property

    Public Property Oferta() As clsInhOferta
        Get
            Return mvarOferta
        End Get
        Set(ByVal value As clsInhOferta)
            mvarOferta = value
        End Set
    End Property

    Public Property WysylkaDelete() As Collection(Of clsInhWysylka)
        Get
            Return mvarWysylkiDelete
        End Get
        Set(ByVal value As Collection(Of clsInhWysylka))
            mvarWysylkiDelete = value
        End Set
    End Property

    Public Property StatusWysylki As clsStatusWysylki
        Get
            Return mvarStatusWysylki
        End Get
        Set(ByVal value As clsStatusWysylki)
            mvarStatusWysylki = value
        End Set
    End Property

#End Region

#Region "Methods"

    Public Sub New()
        MyBase.New()
        mvarPozycjeZlecenia = New Collection(Of clsInhPozycjaZlecenia)
        mvarPozycjeZleceniaDel = New Collection(Of clsInhPozycjaZlecenia)
        mvarWysylki = New Collection(Of clsInhWysylka)
        mvarWysylkiDelete = New Collection(Of clsInhWysylka)
    End Sub

    Public Sub New(ByVal ID As Integer, ByVal lvarDbHolder As clsDataBase)

        MyBase.New(ID, lvarDbHolder, VAR_ERROR_HANDLER)
        mvarPozycjeZlecenia = New Collection(Of clsInhPozycjaZlecenia)
        mvarPozycjeZleceniaDel = New Collection(Of clsInhPozycjaZlecenia)

        mvarKlient = New clsKlient(MyBase.KlientID, lvarDbHolder, VAR_ERROR_HANDLER)
        mvarSposobZamowienia = New clsSposobZamowienia(MyBase.SposobZamowieniaId, lvarDbHolder, VAR_ERROR_HANDLER)
        mvarAdresWysylki = New clsAdresWysylki(MyBase.AdresWysylkiId, lvarDbHolder, VAR_ERROR_HANDLER)
        mvarStatusZlecenia = New clsStatus(MyBase.SatusID, lvarDbHolder, VAR_ERROR_HANDLER)
        mvarPracownik = New clsPracownik(MyBase.PracownikID.ToString(), lvarDbHolder, VAR_ERROR_HANDLER)
        mvarStatusWysylki = New clsStatusWysylki(MyBase.StatusWysylkiID, lvarDbHolder, VAR_ERROR_HANDLER)
        If MyBase.OfertaId <> 0 Then
            mvarOferta = New clsInhOferta(MyBase.OfertaId, lvarDbHolder)
        End If

        Dim lvarTabel As DataTable = clsZlecenie.PobierzWszystkiePozycje(lvarDbHolder, VAR_ERROR_HANDLER, ID)

        If Not lvarTabel Is Nothing Then
            For Each lvarRow As DataRow In lvarTabel.Rows
                mvarPozycjeZlecenia.Add(New clsInhPozycjaZlecenia(CInt(lvarRow.Item("id")), lvarDbHolder))
            Next
        End If


        'WYSYLKA
        mvarWysylki = New Collection(Of clsInhWysylka)
        mvarWysylkiDelete = New Collection(Of clsInhWysylka)

        Dim lvarTabel2 As DataTable = clsZlecenie.PobierzWszystkieWysylki(lvarDbHolder, VAR_ERROR_HANDLER, ID)

        If Not lvarTabel2 Is Nothing Then
            For Each lvarRow As DataRow In lvarTabel2.Rows
                mvarWysylki.Add(New clsInhWysylka(CInt(lvarRow.Item("id")), lvarDbHolder))
            Next
        End If

    End Sub

    Public Overloads Sub Save(ByVal lvarDbHolder As clsDataBase)

        MyBase.Save(lvarDbHolder, VAR_ERROR_HANDLER)

        For Each lvarPozycja As clsInhPozycjaZlecenia In mvarPozycjeZleceniaDel
            lvarPozycja.Delete(lvarDbHolder)
        Next

        For Each lvarPozycja As clsInhPozycjaZlecenia In mvarPozycjeZlecenia
            lvarPozycja.ZlecenieId = MyBase.ID
            lvarPozycja.Cena = Replace(lvarPozycja.Cena, ",", ".")
            lvarPozycja.Save(lvarDbHolder)
        Next

        For Each lvarWysylka As clsInhWysylka In mvarWysylki
            lvarWysylka.ZlecenieID = MyBase.ID
            lvarWysylka.Save(lvarDbHolder)
        Next

        For Each lvarWysylka As clsInhWysylka In mvarWysylkiDelete
            For Each lvarPozycja As clsPozycjeWysylki In lvarWysylka.PozycjeWysylki
                lvarPozycja.Delete(lvarDbHolder, VAR_ERROR_HANDLER)
            Next

            For Each lvarPaczka As clsInhPaczkiWysylki In lvarWysylka.PaczkiWysylki
                lvarPaczka.Delete(lvarDbHolder)
            Next
            For Each lvarPaczka As clsInhPaczkiWysylki In lvarWysylka.PaczkiWysylkiDelete
                lvarPaczka.Delete(lvarDbHolder)
            Next

            lvarWysylka.Delete(lvarDbHolder, VAR_ERROR_HANDLER)
        Next

    End Sub

    Shared Function Produkcja(ByVal lvarDbHolder As clsDataBase, IdZlecenia As Integer) As String

        Dim lvarReader As OdbcDataReader = Nothing
        Dim lvarReturn As String = String.Empty

        If lvarDbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(lvarDbHolder, "queries_application_oem", "cls_zlecenie", 3, VAR_ERROR_HANDLER)
            ' SELECT * FROM oem.tracking(<%p_id_zlecenia%>);
            lvarDbHolder.BeginTrans()
            With lvarSqlRunner
                .AddParameter("p_id_zlecenia", IdZlecenia.ToString())
                lvarReader = .ExecuteQuery
            End With
            lvarDbHolder.CommitTrans()
            If lvarReader.HasRows Then
                While lvarReader.Read
                    lvarReturn = lvarReader.Item(0).ToString()
                End While
            End If
        End If

        Return lvarReturn

    End Function

#End Region

End Class
